package com.yeyaomai.dksns.flowis.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yeyaomai.dksns.flowis.domain.Engine;
import com.yeyaomai.dksns.flowis.service.EngineService;
import com.yeyaomai.dksns.flowis.service.result.EngineXml;
import com.yeyaomai.dksns.flowis.util.CollectionNames;
import com.yeyaomai.dksns.flowis.util.NumberFormatUtils;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

/**
 * 搜索引擎Controller
 * @author sun_shan
 * 
 */

@Controller
@RequestMapping("/engine/*")
public class EngineController {
	
	@Resource
	private EngineService engineService;
	
	//动态时间查询
	private Map<String, String> getThisDay(){
		Map<String, String> map=new HashMap<String, String>();
		String time=VelocityToolDateUtils.getDay(0);
		map.put(time, time);
		return map; 
	}
	
	//搜索引擎详细信息列表统计
	@RequestMapping(value = "search")
	public ModelAndView searchCount(){
		ModelAndView model = new ModelAndView("search-count");
		String fromDate="";
		String toDate="";
		Map<String, String> dateMap=this.getThisDay();
		for(Entry<String, String> entry:dateMap.entrySet()){
			fromDate=entry.getKey();
			toDate=entry.getValue();
		}
		
		List<Engine> list=new ArrayList<Engine>();
		GroupByResults<Engine> engineResult=engineService.findEngineByEngineNameAndTimeBetween(fromDate, toDate);		
		//总计
		long keyWordNumber_totle=0;
		long searchTime_totle=0;
		long uniqueVistor_totle=0;
		long ip_totle=0;
		for (Engine engine : engineResult) {
			list.add(engine);
			keyWordNumber_totle+=engine.getKeyWordNumber();
			searchTime_totle+=engine.getSearchTime();
			uniqueVistor_totle+=engine.getUniqueVistor();
			ip_totle+=engine.getIp();			 
		}
		model.addObject("list", list);
		model.addObject("keyWordNumber_totle", keyWordNumber_totle);
		model.addObject("searchTime_totle", searchTime_totle);
		model.addObject("uniqueVistor_totle", uniqueVistor_totle);
		model.addObject("ip_totle", ip_totle);
		model.addObject("NumberFormatUtils", NumberFormatUtils.class);
		model.addObject("betweenDate", "从"+fromDate+"到"+toDate+"");
		return model;
	}
	
	//搜索引擎统计
	@RequestMapping(value = "pie")
	public void pieXml(HttpServletResponse response) throws IOException {	
		String fromDate="";
		String toDate="";
		Map<String, String> dateMap=this.getThisDay();
		for(Entry<String, String> entry:dateMap.entrySet()){
			fromDate=entry.getKey();
			toDate=entry.getValue();
		}
		List<Engine> engineList=new ArrayList<Engine>();
		GroupByResults<Engine> engineResult=engineService.findEngineByEngineNameAndTimeBetween(fromDate, toDate);
		for (Engine engine : engineResult) {
			engineList.add(engine); 		
		}
		String xml=EngineXml.getPie(engineList,fromDate,toDate);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(xml);  
	}
	
	
	//主要搜索引擎搜索量近30日趋势
	@RequestMapping(value="msLine")
	public void msLineXml(HttpServletResponse response) throws IOException{ 
		List<Map.Entry<String, String>> entryMap=VelocityToolDateUtils.getThirtyday();
		Map<String, Long> baiduMap=new TreeMap<String, Long>();
		Map<String, Long> sougouMap=new TreeMap<String, Long>();
		Map<String, Long> gugeMap=new TreeMap<String, Long>();
		
		for(Map.Entry<String, String> entry:entryMap){
			long l=this.countEngine(CollectionNames.BAIDU, entry.getValue());
			baiduMap.put(entry.getValue(), l);
		}
		for(Map.Entry<String, String> entry:entryMap){
			long l=this.countEngine(CollectionNames.SOUGOU, entry.getValue());
			sougouMap.put(entry.getValue(), l);
		}
		for(Map.Entry<String, String> entry:entryMap){
			long l=this.countEngine(CollectionNames.GUGE, entry.getValue());
			gugeMap.put(entry.getValue(), l);
		}
		String xml=EngineXml.getLine(baiduMap,sougouMap,gugeMap);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(xml);
		
	}
	
	//主要搜索引擎最近30日
	@SuppressWarnings("null")
	private long countEngine(String engineName,String date){
		long l=0;
		if(null!=engineName||!engineName.equals("")){
			if(engineName.equals(CollectionNames.BAIDU)){
				l=engineService.countSearchTimeByEngineNameAndCreateTime(engineName, date);
			}else if(engineName.equals(CollectionNames.SOUGOU)){
				l=engineService.countSearchTimeByEngineNameAndCreateTime(engineName, date);
			}else if(engineName.equals(CollectionNames.GUGE)){
				l=engineService.countSearchTimeByEngineNameAndCreateTime(engineName, date);
			}
		}
		return l;
	}
	
	
	//总搜索量近30日趋势 
	@RequestMapping(value="stackedArea")
	public void stackedAreaXml(HttpServletResponse response) throws IOException{ 
		Map<String,Long> engineMap=this.countSearchTime();
		String xml=EngineXml.getArea(engineMap);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(xml);
	}
	
	//总搜索量最近30日
	private Map<String,Long> countSearchTime(){
		//查询最近30天
		List<Map.Entry<String, String>> map=VelocityToolDateUtils.getThirtyday();
		Map<String, Long> maps=new TreeMap<String, Long>();
		for(Map.Entry<String, String> entry:map){
			long longResult=engineService.countSearchTimeByCreateTime(entry.getValue());
			maps.put(entry.getValue(),longResult);
		}
		return maps;
	}

	

}
